<?php 
class CFlashsales extends Zend_Db_Table
{
	protected $_name;
	protected $_primary;
	public function __construct()
	{
		$this->_name=R_PRE. "flash_sales";
		$this->_primary = 'sales_id';
		parent::__construct();
	}
	
	public function add($data)
	{
		return $this->insert($data);
	}

	public function getSaleItem()
	{
		$rows = $this->fetchAll(array('statu = ?'=>1));
		$itemIds = array();
		foreach ($rows as $v) {
			$itemIds[] = $v->itemid;
		}
		$itemId = implode(',', $itemIds);
		if (!$itemId) {
			return array();
		}
		$itemShow=new CItemShow();
        $itemShow->setType('product');
        $itemShow->setWhere(R_PRE."items.itemid in (" . $itemId . ")");
        $result = $itemShow->getAllItems(); 
        $return = array();
        $flashCommon = new CFlashcommon();
        $sorts = array();
        foreach ($result as $v) {
        	if ($flashCommon->isSuit($v['itemid'])) {
        		$v['is_suit'] = 1;
        	} else {
        		$v['is_suit'] = 0;
        	}
        	$sorts[] = $v['is_suit'];
        	$return[] = $v;
        }   
        array_multisort($sorts,$return);    
        return $return;
	}
	
	public function productSale($beginTime, $endTime)
	{
		$db = $this->getAdapter();
		$db->beginTransaction();
		$return = array();
		try {
			//获取国家队和俱乐部的子分类		
			$sql = 'select flid from zcshop_productfls where pid=92 or pid=93';
			$rows = $db->fetchAll($sql);	
			$flids = array();
			foreach ($rows as $v) {
				$flids[] = $v['flid'];
			}
			shuffle($flids);
			//获取11、12、13的产品id
			$expRows = $db->fetchAll('select itemid from zcshop_product_attr where attr_id_ex in(11,12,13)');
			$exparr = array();
			foreach ($expRows as $v) {
				$exparr[] = $v['itemid'];
			}
			$exp = implode(',', $exparr);
			////获取两个旧产品
			foreach ($flids as $v) {
				if ($exp) {
					$sql = "select p.itemid from zcshop_products p inner join zcshop_items i on i.itemid=p.itemid inner join zcshop_product_attr a on a.itemid=p.itemid  where p.is_showed=1  and p.onsales=0 and (i.subject not like '13-14%' and i.subject not like '13/14%')  and i.subject not like'%#%' and i.flid = $v and p.itemid not in($exp) order by rand() limit 1";
					
				} else {
					$sql = "select p.itemid from zcshop_products p inner join zcshop_items i on i.itemid=p.itemid inner join zcshop_product_attr a on a.itemid=p.itemid  where p.is_showed=1  and p.onsales=0 and (i.subject not like '13-14%' and i.subject not like '13/14%') and i.subject not like'%#%' and i.flid = $v and a.attr_id_ex not in(11,12,13) order by rand() limit 1";
				}
				
				$rows = $db->fetchAll($sql);
				foreach ($rows as $_v) {
					$return[] = $_v;
				}
				if (count($return) == 2) {
					break;
				}
			}
			//获取两个新产品
			$returnnew =array();
			foreach ($flids as $v) {
				if ($exp) {
					$sqlnew = "select p.itemid from zcshop_products p inner join zcshop_items i on i.itemid=p.itemid inner join zcshop_product_attr a on a.itemid=p.itemid  where p.is_showed=1 and i.subject not like'%#%' and p.onsales=0 and (i.subject like '13-14%' or i.subject like '13/14%') and i.flid = $v and p.itemid not in($exp) order by rand() limit 1";
				} else {
					$sqlnew = "select p.itemid from zcshop_products p inner join zcshop_items i on i.itemid=p.itemid inner join zcshop_product_attr a on a.itemid=p.itemid  where p.is_showed=1 and p.onsales=0 and (i.subject like '13-14%' or i.subject like '13/14%') and i.subject not like'%#%' and i.flid = $v and a.attr_id_ex not in(11,12,13) order by rand() limit 1";
				}
				$rows = $db->fetchAll($sqlnew);
				if(!empty($rows)){
					if(!in_array($rows[0]['itemid'], $returnnew)){
						$returnnew[] = $rows[0];
						$newvalues[]=$rows[0]['itemid'];
					}
					if (count($returnnew) == 2) {
						break;
					}
				}
				
			}
			//合并
			$return = array_merge($returnnew,$return);

			/*$leftCount = 4 - count($return);
			echo '<br>' . $leftCount . '<br/>';
			if ($leftCount > 0) {
				$tmp = array();
				foreach ($return as $v) {
					$tmp[] = $v['itemid'];
				}
				$sql = 'select p.itemid from zcshop_products p inner join zcshop_items i on i.itemid=p.itemid where p.is_showed=1 and p.onsales=0 and i.flid in(94,99,104,96,105) and i.itemid not in ('.implode(',', $tmp).') order by rand() limit ' . $leftCount;
				echo $sql;
				$rows = $db->fetchAll($sql);	
				foreach ($rows as $v) {
					$return[] = $v;
				}
			} else {
				for($i=0; $i<abs($leftCount);$i++) {
					$num = rand(0, count($return)-1);		
					$return = array_values($return);
					//print_r($return);			
					unset($return[$num]);
				}
			}*/		
			$price = new CProductPrice();		
			$productAttr = new CProductattr();
			foreach ($return as $v) {
				//$discount = rand(5,15) / 10;
				$sql3 = 'select * from zcshop_products where itemid=' . $v['itemid'];
				$itemRow = $db->fetchRow($sql3);
				$discount = 19.99 / $itemRow['max_price'];	
				$priceAll = $price->getAllPrice($v['itemid']);				
				if ($productAttr->getAttr($v['itemid'], 12)) {
					$discount = 0;
					$i = 0;
					if (count($priceAll) == 4) {
						$sql4 = 'update zcshop_products set min_price=28.99 where itemid=' . $v['itemid'];
						$db->query($sql4);
						foreach ($priceAll as $_v) {
							if ($i == 1) {
								$sql2 = 'update zcshop_product_price set supply_price=33.99, net_price=33.99 where priceid=' . $_v['priceid'];
								$db->query($sql2);
							}
							if ($i == 2) {
								$sql2 = 'update zcshop_product_price set supply_price=30.99, net_price=30.99 where priceid=' . $_v['priceid'];
								$db->query($sql2);
							}
							if ($i == 3) {
								$sql2 = 'update zcshop_product_price set supply_price=28.99, net_price=28.99 where priceid=' . $_v['priceid'];
								$db->query($sql2);
							}
							$i++;
						}
					}
				} elseif ($productAttr->getAttr($v['itemid'], 11)) {
					$discount = 0;
					$i = 0;
				
					if (count($priceAll) == 4) {
						$sql4 = 'update zcshop_products set min_price=23.99 where itemid=' . $v['itemid'];
						$db->query($sql4);
						foreach ($priceAll as $_v) {
							if ($i == 1) {
								$sql2 = 'update zcshop_product_price set supply_price=28.99, net_price=28.99 where priceid=' . $_v['priceid'];
								$db->query($sql2);
							}
							if ($i == 2) {
								$sql2 = 'update zcshop_product_price set supply_price=25.99, net_price=25.99 where priceid=' . $_v['priceid'];
								$db->query($sql2);
							}
							if ($i == 3) {
								$sql2 = 'update zcshop_product_price set supply_price=23.99, net_price=23.99 where priceid=' . $_v['priceid'];
								$db->query($sql2);
							}
							
							$i++;
						}
					
					}
				} else {
					
					
					if(in_array($v['itemid'],$newvalues)){
						$maxPrice=23.99;
						$discount = 23.99/$itemRow['max_price'];
					}else{
						$maxPrice = $itemRow['max_price'] * $discount;
					}
					$sql4 = 'update zcshop_products set min_price=' . $maxPrice . ', max_price=' . $maxPrice . ' where itemid=' . $v['itemid'];
					$db->query($sql4);
					foreach ($priceAll as $_v) {
						$sql2 = 'update zcshop_product_price set supply_price=' . $maxPrice . ', net_price=' . $maxPrice . ' where priceid=' . $_v['priceid'];
						$db->query($sql2);
					}
				}			
				
				$this->insert(array('itemid'=>$v['itemid'], 'price_org'=>serialize($priceAll), 'min_price'=>$itemRow['min_price'], 'max_price'=>$itemRow['max_price'], 'discount'=>$discount, 'begintime'=>$beginTime, 'endtime'=>$endTime,'statu'=>1));
			}
			
			//团购
			shuffle($flids);
			foreach ($flids as $v) {
				$sql = 'select p.itemid from zcshop_products p inner join zcshop_items i on i.itemid=p.itemid inner join zcshop_product_attr a on a.itemid=p.itemid  where p.is_showed=1 and p.onsales=0 and i.flid = '.$v.' and a.attr_id_ex in(11,12) order by rand() limit 1';
				//echo $sql . '<br>';
				$rows = $db->fetchAll($sql);	
				foreach ($rows as $_v) {
					$return2[] = $_v;
				}
				if (count($return2) == 4) {
					break;
				}
			}
			
			$leftCount = 4 - count($return2);
			echo '<br>' . $leftCount . '<br/>';
			if ($leftCount > 0) {
			} else {
				for($i=0; $i<abs($leftCount);$i++) {
					$num = rand(0, count($return2)-1);		
					$return2 = array_values($return2);
					//print_r($return);			
					unset($return2[$num]);
				}
			}			
			
			$price = new CProductPrice();		
			$productAttr = new CProductattr();
			foreach ($return2 as $v) {
				//$discount = rand(5,15) / 10;
				$sql3 = 'select * from zcshop_products where itemid=' . $v['itemid'];
				$itemRow = $db->fetchRow($sql3);
				//判断是不是新款
				$sqlcheck = 'select subject from zcshop_items where itemid=' . $v['itemid'];
				$checkrow = $db->fetchRow($sqlcheck);
				$match= substr(trim($checkrow['subject']), 0,5);
				if($match=='13/14' || $match=='13-14'){
					$discount = 23.99 / $itemRow['max_price'];	
				}else{
					$discount = 19.99 / $itemRow['max_price'];
				}
				$priceAll = $price->getAllPrice($v['itemid']);				
				if ($productAttr->getAttr($v['itemid'], 12)) {
					$discount = 0;
					$i = 0;
					if (count($priceAll) == 4) {
						$sql4 = 'update zcshop_products set min_price=28.99 where itemid=' . $v['itemid'];
						$db->query($sql4);
						foreach ($priceAll as $_v) {
							if ($i == 1) {
								$sql2 = 'update zcshop_product_price set supply_price=33.99, net_price=33.99 where priceid=' . $_v['priceid'];
								$db->query($sql2);
							}
							if ($i == 2) {
								$sql2 = 'update zcshop_product_price set supply_price=30.99, net_price=30.99 where priceid=' . $_v['priceid'];
								$db->query($sql2);
							}
							if ($i == 3) {
								$sql2 = 'update zcshop_product_price set supply_price=28.99, net_price=28.99 where priceid=' . $_v['priceid'];
								$db->query($sql2);
							}
							$i++;
						}
					}
				} elseif ($productAttr->getAttr($v['itemid'], 11)) {
					$discount = 0;
					$i = 0;
				
					if (count($priceAll) == 4) {
						$sql4 = 'update zcshop_products set min_price=23.99 where itemid=' . $v['itemid'];
						$db->query($sql4);
						foreach ($priceAll as $_v) {
							if ($i == 1) {
								$sql2 = 'update zcshop_product_price set supply_price=28.99, net_price=28.99 where priceid=' . $_v['priceid'];
								$db->query($sql2);
							}
							if ($i == 2) {
								$sql2 = 'update zcshop_product_price set supply_price=25.99, net_price=25.99 where priceid=' . $_v['priceid'];
								$db->query($sql2);
							}
							if ($i == 3) {
								$sql2 = 'update zcshop_product_price set supply_price=23.99, net_price=23.99 where priceid=' . $_v['priceid'];
								$db->query($sql2);
							}
							
							$i++;
						}
					
					}
				} else {
					
					$maxPrice = $itemRow['max_price'] * $discount;
					$sql4 = 'update zcshop_products set min_price=' . $maxPrice . ', max_price=' . $maxPrice . ' where itemid=' . $v['itemid'];
					$db->query($sql4);
					foreach ($priceAll as $_v) {
						$sql2 = 'update zcshop_product_price set supply_price=' . $maxPrice . ', net_price=' . $maxPrice . ' where priceid=' . $_v['priceid'];
						$db->query($sql2);
					}
				}			
				
				$this->insert(array('itemid'=>$v['itemid'], 'price_org'=>serialize($priceAll), 'min_price'=>$itemRow['min_price'], 'max_price'=>$itemRow['max_price'], 'discount'=>$discount, 'begintime'=>$beginTime, 'endtime'=>$endTime,'statu'=>1));
			}
			$db->commit();
		} catch (Exception $e) {
			$db->rollback();
		}
		CCache::flushCombinlistItemMap();
	}
	
	public function resumeProductPrice()
	{
		$rows = $this->fetchAll(array('statu = ?'=>1));
		$price = new CProductPrice();
		foreach ($rows as $v) {
			$priceAll = unserialize($v->price_org);
			foreach ($priceAll as $_v) {
				$price->update(array('supply_price'=>$_v['supply_price'], 'net_price'=>$_v['net_price']), 'priceid=' . $_v['priceid']);
			}
			$sql = 'update zcshop_products set min_price=' . $v->min_price . ', max_price=' . $v->max_price . ' where itemid=' . $v->itemid;
			$this->getAdapter()->query($sql);
		}
		$this->update(array('statu'=>0), 'statu=1');
		
		$flashItem = new CFlashitem();
		$flashItem->update(array('statu'=>0), 'statu=1');
	}
	
	public function resumeExpirePrice()
	{
		$time = strtotime(date('Y-m-d H:i', time()));
		$rows = $this->fetchAll(array('statu = ?'=>1, 'endtime <= ?'=>$time));
		$price = new CProductPrice();
		foreach ($rows as $v) {
			$priceAll = unserialize($v->price_org);
			foreach ($priceAll as $_v) {
				$price->update(array('supply_price'=>$_v['supply_price'], 'net_price'=>$_v['net_price']), 'priceid=' . $_v['priceid']);
			}
			$sql = 'update zcshop_products set min_price=' . $v->min_price . ', max_price=' . $v->max_price . ' where itemid=' . $v->itemid;
			$this->getAdapter()->query($sql);
		}
		if ($this->update(array('statu'=>0), 'statu=1 and endtime <= ' . $time)) {
			CCache::flushCombinlistItemMap();
			$flashItem = new CFlashitem();
			$flashItem->update(array('statu'=>0), 'statu=1 and expiretime<= ' . $time);
		}
	}
	
	public function getTime($type = 'start' )
	{
		$file = MY_ROOT . '/php_config/flash_sale_time.ini';
		$arr = parse_ini_file($file, 1);
		$time = strtotime(date('Y-m-d H:i:s', time()));
		$date = date('Y-m-d', time());
		if ($type == 'start') {
			foreach ($arr as $v) {			
				$beginTime = strtotime($date . ' ' . $v['begin']);
				if ($beginTime > $time) {
					return $beginTime;
				}
			}
			
			return strtotime(date('Y-m-d', time()+3600*24) . ' ' . $arr[0]['begin']);			
		} else if ($type == 'end') {
			foreach ($arr as $v) {			
				$beginTime = strtotime($date . ' ' . $v['begin']);
				$endTime = strtotime($date . ' ' . $v['end']);

				if ($time > $beginTime && $time < $endTime) {
					return $endTime;
				}
			}
			return -1;
		}
		return 0;
	}
	
	public function getCurrentBeginTime()
	{
		$row = $this->fetchRow(array('statu = ?'=>1));
		if ($row) {
			return $row->begintime;
		}
		return 0;
	}
	
	public function isFlashItem($itemid)
	{
		$row = $this->fetchRow(array('statu = ?'=>1, 'itemid = ?'=>$itemid));
		if ($row) {
			return true;
		}
		return false;
	}
	
	public function getDiscount($itemid)
	{
		$db = $this->getAdapter();
		if (empty($itemid)) {
			return false;
		}
		$sql3 = 'select * from zcshop_products where itemid=' . $itemid;
		$itemRow = $db->fetchRow($sql3);
		$sql4 = 'select subject from zcshop_items where itemid=' . $itemid;
		$row = $db->fetchRow($sql4);
		$match= substr(trim($row['subject']), 0,5);
		if($match=='13/14' || $match=='13-14'){
			$discount= 23.99/ $itemRow['max_price'];
		}else{
			$discount = 19.99 / $itemRow['max_price'];
		}
		$productAttr = new CProductattr();		
					
		if ($productAttr->getAttr($itemid, 12) || $productAttr->getAttr($itemid, 11)) {
			$discount = 0.8;
		}
		
		return $discount;
	}
}